﻿//****************************************************************************************************
//
// Copyright © ProFast Computing 2012-2016
//
//****************************************************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using System.IO;
using AppGlobals;
using PFRandomDataExt;
using PFTextObjects;
using PFRandomDataProcessor;

namespace PFRandomValueDataTables
{
    /// <summary>
    /// Contains routines to generate random Byte values to an ADO.NET DataTable object.
    /// </summary>
    public class RandomBytesDataTable
    {
        //private work variables
        private StringBuilder _msg = new StringBuilder();

        //private variables for properties

        //constructors
        /// <summary>
        /// Default constructor.
        /// </summary>
        public RandomBytesDataTable()
        {
            ;
        }

        //properties

        //methods

        /// <summary>
        /// Creates a DataTable containing a set of random byte values.
        /// </summary>
        /// <param name="numRows">Num of rows with byte values to generate.</param>
        /// <param name="dataRequest">RandomBytesDataRequest object contains the definition for how to generate the random bytes.</param>
        /// <returns>ADO.NET DataTable containing the set of random values.</returns>
        public DataTable CreateRandomDataTable(int numRows, RandomBytesDataRequest dataRequest)
        {
            DataTable dt = null;

            if (dataRequest.OutputArrayOfValues)
            {
                if (dataRequest.OutputByte)
                {
                    dt = CreateByteArrayDataTable(numRows, dataRequest.MinArrayLength, dataRequest.MaxArrayLength);
                }
                else
                {
                    dt = CreateCharArrayDataTable(numRows, dataRequest.MinArrayLength, dataRequest.MaxArrayLength);
                }
            }
            else // (OutputSingleValue)
            {
                if (dataRequest.OutputByte)
                {
                    dt = CreateByteValueDataTable(numRows);
                }
                else
                {
                    dt = CreateCharValueDataTable(numRows);
                }
            }

            return dt;
        }

        /// <summary>
        /// Creates a DataTable containing a set of random byte array values.
        /// </summary>
        /// <param name="numRows">Num of rows with byte array values to generate.</param>
        /// <param name="minArrayLength">Minimum length for each generated byte array.</param>
        /// <param name="maxArrayLength">Maximum length for each generated byte array.</param>
        /// <returns>ADO.NET DataTable containing the set of random values.</returns>
        public DataTable CreateByteArrayDataTable(int numRows, string minArrayLength, string maxArrayLength)
        {
            DataTable dt = new DataTable();
            RandomNumber rn = new RandomNumber();
            RandomBytes rb = new RandomBytes();

            try
            {
                int minimumArrayLength = Convert.ToInt32(minArrayLength);
                int maximumArrayLength = Convert.ToInt32(maxArrayLength);

                DataColumn dc0 = new DataColumn("RandomValue");
                dc0.DataType = Type.GetType("System.Byte[]"); 
                dt.Columns.Add(dc0);

                for (int i = 0; i < numRows; i++)
                {
                    DataRow dr = dt.NewRow();
                    int arrayLength = rn.GenerateRandomInt(minimumArrayLength, maximumArrayLength);
                    byte[] b = rb.GenerateRandomBytes(arrayLength);
                    dr[0] = b;
                    dt.Rows.Add(dr);
                }

            }
            catch (System.Exception ex)
            {
                _msg.Length = 0;
                _msg.Append("Error in CreateByteArrayDataTable routine.\r\n");
                _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
                throw new System.Exception(_msg.ToString());
            }
            finally
            {
                ;
            }

            return dt;

        }

        /// <summary>
        /// Creates a DataTable containing a set of random byte array values. Includes extra data (string and hex representations) for displaying the byte array on a form with a grid.
        /// </summary>
        /// <param name="numRows">Num of rows with byte array values to generate.</param>
        /// <param name="minArrayLength">Minimum length for each generated byte array.</param>
        /// <param name="maxArrayLength">Maximum length for each generated byte array.</param>
        /// <returns>ADO.NET DataTable containing the set of random values.</returns>
        /// <remarks>This routine shows the generated byte arrays in alternate string and hex formats.</remarks>
        public DataTable CreateByteArrayPreviewDataTable(int numRows, string minArrayLength, string maxArrayLength)
        {
            DataTable dt = new DataTable();
            RandomNumber rn = new RandomNumber();
            RandomBytes rb = new RandomBytes();

            try
            {
                int minimumArrayLength = Convert.ToInt32(minArrayLength);
                int maximumArrayLength = Convert.ToInt32(maxArrayLength);

                DataColumn dc0 = new DataColumn("RandomValue");
                dc0.DataType = Type.GetType("System.String");
                //dc0.DataType = Type.GetType(""Byte");  //Byte[] does not blowup DataGridView; System.Byte[] does.
                dt.Columns.Add(dc0);
                DataColumn dc1 = new DataColumn("StringRepresentation");
                dc1.DataType = Type.GetType("System.String");
                dt.Columns.Add(dc1);
                DataColumn dc2 = new DataColumn("HexRepresentation");
                dc2.DataType = Type.GetType("System.String");
                dt.Columns.Add(dc2);

                for (int i = 0; i < numRows; i++)
                {
                    DataRow dr = dt.NewRow();
                    int arrayLength = rn.GenerateRandomInt(minimumArrayLength, maximumArrayLength);
                    byte[] b = rb.GenerateRandomBytes(arrayLength);
                    string s1 = PFTextObjects.PFTextProcessor.ConvertByteArrayToAsciiExtendedString(b);
                    string s2 = PFTextObjects.PFTextProcessor.ConvertByteArrayToHexString(b);
                    dr[0] = "System.Byte[] Array";
                    dr[1] = s1;
                    dr[2] = s2;
                    dt.Rows.Add(dr);
                }

            }
            catch (System.Exception ex)
            {
                _msg.Length = 0;
                _msg.Append("Error in CreateByteArrayPreviewDataTable routine.\r\n");
                _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
                throw new System.Exception(_msg.ToString());
            }
            finally
            {
                ;
            }

            return dt;

        }

        /// <summary>
        /// Creates a DataTable containing a set of random char array values.
        /// </summary>
        /// <param name="numRows">Num of rows with char array values to generate.</param>
        /// <param name="minArrayLength">Minimum length for each generated char array.</param>
        /// <param name="maxArrayLength">Maximum length for each generated char array.</param>
        /// <returns>ADO.NET DataTable containing the set of random values.</returns>
        public DataTable CreateCharArrayDataTable(int numRows, string minArrayLength, string maxArrayLength)
        {
            DataTable dt = new DataTable();
            RandomNumber rn = new RandomNumber();
            RandomBytes rb = new RandomBytes();

            try
            {
                int minimumArrayLength = Convert.ToInt32(minArrayLength);
                int maximumArrayLength = Convert.ToInt32(maxArrayLength);

                DataColumn dc0 = new DataColumn("RandomValue");
                dc0.DataType = Type.GetType("System.Char[]");
                dt.Columns.Add(dc0);

                for (int i = 0; i < numRows; i++)
                {
                    DataRow dr = dt.NewRow();
                    int arrayLength = rn.GenerateRandomInt(minimumArrayLength, maximumArrayLength);
                    char[] c = rb.GenerateRandomChars(arrayLength);
                    dr[0] = c;
                    dt.Rows.Add(dr);
                }

            }
            catch (System.Exception ex)
            {
                _msg.Length = 0;
                _msg.Append("Error in CreateCharArrayDataTable routine.\r\n");
                _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
                throw new System.Exception(_msg.ToString());
            }
            finally
            {
                ;
            }

            return dt;

        }

        /// <summary>
        /// Creates a DataTable containing a set of random char array values. Includes extra data (string and hex representations) for displaying the char array on a form with a grid.
        /// </summary>
        /// <param name="numRows">Num of rows with char array values to generate.</param>
        /// <param name="minArrayLength">Minimum length for each generated char array.</param>
        /// <param name="maxArrayLength">Maximum length for each generated char array.</param>
        /// <returns>ADO.NET DataTable containing the set of random values.</returns>
        /// <remarks>This routine shows the generated char arrays in alternate string and hex formats.</remarks>
        public DataTable CreateCharArrayPreviewDataTable(int numRows, string minArrayLength, string maxArrayLength)
        {
            DataTable dt = new DataTable();
            RandomNumber rn = new RandomNumber();
            RandomBytes rb = new RandomBytes();

            try
            {
                int minimumArrayLength = Convert.ToInt32(minArrayLength);
                int maximumArrayLength = Convert.ToInt32(maxArrayLength);

                DataColumn dc0 = new DataColumn("RandomValue");
                dc0.DataType = Type.GetType("System.String");
                //dc0.DataType = Type.GetType("System.Char[]");  //Grid displays no column for System.Char[]
                dt.Columns.Add(dc0);
                DataColumn dc1 = new DataColumn("StringRepresentation");
                dc1.DataType = Type.GetType("System.String");
                dt.Columns.Add(dc1);
                DataColumn dc2 = new DataColumn("HexRepresentation");
                dc2.DataType = Type.GetType("System.String");
                dt.Columns.Add(dc2);

                for (int i = 0; i < numRows; i++)
                {
                    DataRow dr = dt.NewRow();
                    int arrayLength = rn.GenerateRandomInt(minimumArrayLength, maximumArrayLength);
                    char[] c = rb.GenerateRandomChars(arrayLength);
                    string s1 = PFTextObjects.PFTextProcessor.ConvertCharArrayToString(c);
                    string s2 = PFTextObjects.PFTextProcessor.ConvertCharArrayToHexString(c);
                    dr[0] = "System.Char[] Array";
                    dr[1] = s1;
                    dr[2] = s2;
                    dt.Rows.Add(dr);
                }

            }
            catch (System.Exception ex)
            {
                _msg.Length = 0;
                _msg.Append("Error in CreateCharArrayPreviewDataTable routine.\r\n");
                _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
                throw new System.Exception(_msg.ToString());
            }
            finally
            {
                ;
            }

            return dt;

        }

        //routines for single values follow
        
        /// <summary>
        /// Creates set of random byte values.
        /// </summary>
        /// <param name="numRows">Number of random byte values to generate.</param>
        /// <returns>ADO.NET DataTable containing the set of random values.</returns>
        public DataTable CreateByteValueDataTable(int numRows)
        {
            DataTable dt = new DataTable();
            RandomBytes rb = new RandomBytes();

            try
            {
                DataColumn dc0 = new DataColumn("RandomValue");
                dc0.DataType = Type.GetType("System.Byte");
                dt.Columns.Add(dc0);

                for (int i = 0; i < numRows; i++)
                {
                    DataRow dr = dt.NewRow();
                    byte b = rb.GenerateRandomByte();
                    dr[0] = b;
                    dt.Rows.Add(dr);
                }

            }
            catch (System.Exception ex)
            {
                _msg.Length = 0;
                _msg.Append("Error in CreateByteValueDataTable routine.\r\n");
                _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
                throw new System.Exception(_msg.ToString());
            }
            finally
            {
                ;
            }

            return dt;

        }

        /// <summary>
        /// Creates set of random byte values. Includes routines for displaying the random bytes on a form with a grid.
        /// </summary>
        /// <param name="numRows">Number of random byte values to generate.</param>
        /// <returns>ADO.NET DataTable containing the set of random values.</returns>
        /// <remarks>This routine shows the generated bytes in alternate string and hex formats.</remarks>
        public DataTable CreateByteValuePreviewDataTable(int numRows)
        {
            DataTable dt = new DataTable();
            RandomBytes rb = new RandomBytes();

            try
            {

                DataColumn dc0 = new DataColumn("RandomValue");
                dc0.DataType = Type.GetType("System.String");
                //dc0.DataType = Type.GetType(""Byte");  //Byte[] does not blowup DataGridView; System.Byte[] does.
                dt.Columns.Add(dc0);
                DataColumn dc1 = new DataColumn("StringRepresentation");
                dc1.DataType = Type.GetType("System.String");
                dt.Columns.Add(dc1);
                DataColumn dc2 = new DataColumn("HexRepresentation");
                dc2.DataType = Type.GetType("System.String");
                dt.Columns.Add(dc2);

                for (int i = 0; i < numRows; i++)
                {
                    DataRow dr = dt.NewRow();
                    byte b = rb.GenerateRandomByte();
                    string s1 = b.ToString();
                    string s2 = PFTextObjects.PFTextProcessor.ConvertByteArrayToHexString(new byte[] { b });
                    dr[0] = "System.Byte";
                    dr[1] = s1;
                    dr[2] = s2;
                    dt.Rows.Add(dr);
                }

            }
            catch (System.Exception ex)
            {
                _msg.Length = 0;
                _msg.Append("Error in CreateByteValuePreviewDataTable routine.\r\n");
                _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
                throw new System.Exception(_msg.ToString());
            }
            finally
            {
                ;
            }

            return dt;

        }

        /// <summary>
        /// Creates set of random char values.
        /// </summary>
        /// <param name="numRows">Number of random char values to generate.</param>
        /// <returns>ADO.NET DataTable containing the set of random values.</returns>
        public DataTable CreateCharValueDataTable(int numRows)
        {
            DataTable dt = new DataTable();
            RandomBytes rb = new RandomBytes();

            try
            {
                DataColumn dc0 = new DataColumn("RandomValue");
                dc0.DataType = Type.GetType("System.Char");
                dt.Columns.Add(dc0);

                for (int i = 0; i < numRows; i++)
                {
                    DataRow dr = dt.NewRow();
                    char c = rb.GenerateRandomChar();
                    dr[0] = c;
                    dt.Rows.Add(dr);
                }

            }
            catch (System.Exception ex)
            {
                _msg.Length = 0;
                _msg.Append("Error in CreateCharValueDataTable routine.\r\n");
                _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
                throw new System.Exception(_msg.ToString());
            }
            finally
            {
                ;
            }

            return dt;

        }

        /// <summary>
        /// Creates set of random char values. Includes routines for displaying the random chars on a form with a grid.
        /// </summary>
        /// <param name="numRows">Number of random char values to generate.</param>
        /// <returns>ADO.NET DataTable containing the set of random values.</returns>
        /// <remarks>This routine shows the generated chars in alternate string and hex formats.</remarks>
        public DataTable CreateCharValuePreviewDataTable(int numRows)
        {
            DataTable dt = new DataTable();
            RandomBytes rb = new RandomBytes();

            try
            {
                DataColumn dc0 = new DataColumn("RandomValue");
                dc0.DataType = Type.GetType("System.String");
                dt.Columns.Add(dc0);
                DataColumn dc1 = new DataColumn("StringRepresentation");
                dc1.DataType = Type.GetType("System.String");
                dt.Columns.Add(dc1);
                DataColumn dc2 = new DataColumn("HexRepresentation");
                dc2.DataType = Type.GetType("System.String");
                dt.Columns.Add(dc2);

                for (int i = 0; i < numRows; i++)
                {
                    DataRow dr = dt.NewRow();
                    char c = rb.GenerateRandomChar();
                    string s1 = Convert.ToString(c);
                    string s2 = PFTextObjects.PFTextProcessor.ConvertCharArrayToHexString(new char[] {c});
                    dr[0] = "System.Char";
                    dr[1] = s1;
                    dr[2] = s2;
                    dt.Rows.Add(dr);
                }

            }
            catch (System.Exception ex)
            {
                _msg.Length = 0;
                _msg.Append("Error in CreateCharValuePreviewDataTable routine.\r\n");
                _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
                throw new System.Exception(_msg.ToString());
            }
            finally
            {
                ;
            }

            return dt;

        }




    }//end class
}//end namespace
